//
//  ViewController.m
//  itheima_寻宝之旅_4.5(OC)
//
//  Created by 肖祺 on 16/4/5.
//  Copyright © 2016年 肖祺. All rights reserved.
//

#import "ViewController.h"
#import "CZHeadSetting.h"
#import "Masonry.h"
#import "CZModelGroup.h"
#import "CZModel.h"
#import "groupController.h"
#import "moneyController.h"
#import "ticketController.h"
#import "giveTicketController.h"
#import "VIPController.h"
#import "AccountController.h"
#import "editController.h"
#import "settingController.h"

#define CZdequeueReusableCell @"ReusableCell"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *headView;

//设置数据源
@property (nonatomic, strong) NSArray *array;

@property (nonatomic, strong) UITableView *tableView;

//scroView


@end
@implementation ViewController

/**
 *  懒加载
 */

-(NSArray *)array{
    
    if (_array == nil) {
        
        _array = [CZModelGroup Model];
    }
    
    return _array;
    
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
 
    
    [self setupTableView];
    

    
}
//设置tableview
-(void)setupTableView{
    
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 1200) style:UITableViewStyleGrouped];
    
   [self.view addSubview:tableView];
    
    tableView.translatesAutoresizingMaskIntoConstraints = false;
    
    //添加约束
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        

        make.top.equalTo(self.view);
        make.right.leading.equalTo(self.view);
       
        make.height.mas_equalTo([UIScreen mainScreen].bounds.size.height);
        
        
    }];
    
    tableView.backgroundColor = [UIColor colorWithRed:212/255.0 green:212/255.0 blue:212/255.0 alpha:1];
    
    
    tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    
    
    
    tableView.delegate = self;
    
    tableView.dataSource = self;
    
 
    
    self.tableView = tableView;
    
    [self setupUI];
    
 
}

//头部view
-(void)setupUI{
    
  
    CZHeadSetting *CZHead = [CZHeadSetting loadHeadXib];
    
    CZHead.backgroundColor = [UIColor whiteColor];
    
    CZHead.accountName.text = @"北极星辰";
    CZHead.greatNum.text = @"被赞12次";
    CZHead.NumOfLikers.text = @"粉丝32位";
    

    CZHead.iconImage.layer.cornerRadius = 35;
    CZHead.iconImage.clipsToBounds = YES;
    
    [self.tableView addSubview:CZHead];
    
   
    [CZHead mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(190);
        make.top.mas_equalTo(-6);
        make.left.right.equalTo(self.view);
    }];
//
    
}

#pragma -mark实现tableview的数据源代理方法

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return self.array.count;
    
  
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    CZModelGroup *model = self.array[section];
    

    return model.setting.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 1) {
        
        return 100;
    }
    
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //使用系统的cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CZdequeueReusableCell];
    
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CZdequeueReusableCell];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    CZModelGroup *modelGroup = self.array[indexPath.section];
    //数据
    CZModel *model = modelGroup.setting[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:model.iconImage];
    cell.textLabel.text = model.name;
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section == 1) {
        
        return 1;
    }
    
    return 190;
    
}


//选中了某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CZModelGroup *group = self.array[indexPath.section];
    CZModel *model = group.setting[indexPath.row];
    
    NSString *VcName = model.VcName;

    if ([VcName isEqualToString:@"groupController"]) {
        
        groupController *controller = [[groupController alloc]init];
        controller.title = @"众筹";
        
        [self.navigationController pushViewController:controller animated:YES];
        
        
    }else if ([VcName isEqualToString:@"moneyController"]) {
        
        moneyController *controller = [[moneyController alloc]init];
         controller.title = @"我的钱包";
        
        [self.navigationController pushViewController:controller animated:YES];
        
        
    }else if ([VcName isEqualToString:@"ticketController"]) {
        
        ticketController *controller = [[ticketController alloc]init];
         controller.title = @"我的优惠券";
        
        [self.navigationController pushViewController:controller animated:YES];
        
        
    }else if ([VcName isEqualToString:@"giveTicketController"]) {
        
        giveTicketController *controller = [[giveTicketController alloc]init];
         controller.title = @"众邀请送优惠券";
        
        [self.navigationController pushViewController:controller animated:YES];
        
        
    }else if ([VcName isEqualToString:@"VIPController"]) {
        
        VIPController *controller = [[VIPController alloc]init];
         controller.title = @"VIP认证";
        
        [self.navigationController pushViewController:controller animated:YES];
        
        
    }else if ([VcName isEqualToString:@"AccountController"]) {
        
        AccountController *controller = [[AccountController alloc]init];
         controller.title = @"账号绑定";
        
        [self.navigationController pushViewController:controller animated:YES];
        
        
    }else if ([VcName isEqualToString:@"editController"]) {
        
        editController *controller = [[editController alloc]init];
         controller.title = @"编辑资料";
        
        [self.navigationController pushViewController:controller animated:YES];
        
        
    }else if ([VcName isEqualToString:@"settingController"]) {
        
        settingController *controller = [[settingController alloc]init];
         controller.title = @"设置";
        
        [self.navigationController pushViewController:controller animated:YES];
        
        
    }

    
    
    

    
    
    
    
    
    
}


@end
